import Dialog from '../../component/vant/dialog/dialog';

var app = getApp();
const util = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    grades: [],
    grade_id: 0,
    pre_url: '/pages/index/index'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var page = this;
    var pre_url = '';
    if (options != undefined && options.url != undefined) {
      pre_url = options.url;
    } else {
      pre_url = '/pages/index/index';
    }
    page.setData({
      pre_url: pre_url
    });

    // app.sendRequest({
    //   url: app.api.public.public_mechanism_list,
    //   success: function(res) {
    //     var schools = [{ id: 0, name: '请选择学校' }];
    //     var school_list = res.data.list || [];
    //     school_list.map((school, i) => {
    //       schools.push({ id: school.id, name: school.name });
    //     });
    //     page.setData({
    //       schools: schools
    //     });
    //   }
    // });
    app.sendRequest({
      url: app.api.public.public_grade_list,
      success: function(res) {
        var grade_list = res.data.list || [],
          primary = [],
          junior = [],
          senior = [];
        grade_list.map(grade => {
          let g_type = grade.grade_id.toString().substr(0, 1);
          if (g_type == '1') {
            primary.push(grade);
          } else if (g_type == '2') {
            junior.push(grade);
          } else if (g_type == '3') {
            senior.push(grade);
          }
        });
        page.setData({
          primary: primary,
          junior: junior,
          senior: senior
        });
      }
    });
    // var classes = [{ id: 0, class_name: '请选择班级' }];
    // for (var i = 1; i < 30; i++) {
    //   classes.push({ id: i, class_name: util.NumberToChinese(i) + '班' });
    // }
    // page.setData({
    //   classes: classes
    // });
  },
  chooseGrade: function(e) {
    var page = this;
    var grade_id = e.currentTarget.dataset.select;
    page.setData({
      grade_id: grade_id
    });
  },
  bindPickerChange: function(e) {
    var page = this;
    var type = e.currentTarget.dataset.type;
    switch (type) {
      // case '0':
      //   page.setData({
      //     index: e.detail.value,
      //     school_err: false
      //   });
      //   break;
      case '1':
        page.setData({
          g_index: e.detail.value,
          grade_err: false
        });
        break;
      // case '2':
      //   page.setData({
      //     c_index: e.detail.value,
      //     class_err: false
      //   });
      //   break;
    }
  },
  nameChange(e) {
    var val = e.detail,
      page = this;
    page.setData({
      name: val
    });
  },
  phoneChange(e) {
    var val = e.detail,
      page = this;
    page.setData({
      phone_err: false,
      phone_err_msg: '',
      phone: val
    });
  },
  infoCheck() {
    var page = this;
    let grade_id = page.data.grade_id;

    //年级
    if (grade_id == 0) {
      Dialog.alert({
        message: '请先选择年级。'
      });
      return false;
    }

    return true;
  },
  sureChoose: function() {
    var page = this;

    let old_student_info = app.globalData.old_student_info;

    //基础信息check
    if (page.infoCheck()) {
      let grade_id = page.data.grade_id;
      let avatar = wx.getStorageSync('userInfo').avatarUrl;

      var params = {
        grade_id: grade_id
      };

      if (old_student_info.avatar == '') {
        params.avatar = avatar;
      }
      if (old_student_info.student_name != '') {
        params.name = old_student_info.student_name;
      }
      if (old_student_info.mobile != '') {
        params.mobile = old_student_info.mobile;
      }

      app.sendRequest({
        url: app.api.school.public_student_info_finish,
        data: params,
        method: 'POST',
        header: {
          token: wx.getStorageSync('access_token'),
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
          var url = page.data.pre_url;
          app.forwardPage(url);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
